package 字符串.easy;

import java.util.Arrays;
import java.util.Stack;

/**
 * @author Lw中
 * @date 2020/8/19 10:06
 */
public class JiuYiQi {

    public static String reverseOnlyLetters(String S) {
        Stack<Character> characters = new Stack<>();
        for (char c : S.toCharArray()) {
            // 如果是字符进栈
            if (Character.isLetter(c)) {
                characters.push(c);
            }
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (char c :S.toCharArray()) {
            // 如果是字母就出栈
            if (Character.isLetter(c)) {
                // 将出栈元素添加至buffer
                stringBuffer.append(characters.pop());
            } else {
                // 如果是字符“-”就直接将符号添加至buffer
                stringBuffer.append(c);
            }
        }
        return stringBuffer.toString();
    }

    public static void main(String[] args) {
        String s = reverseOnlyLetters("a-bC-dEf-ghIj");
        System.out.println(s);
    }

}
